<?php

/**
 * This is the model class for table "usuari".
 *
 * The followings are the available columns in table 'usuari':
 * @property integer $idUsuari
 * @property string $nomUsuari
 * @property string $contrasenya
 * @property string $rol
 * @property string $nomReal
 * @property integer $idClub
 *
 * The followings are the available model relations:
 * @property Club $idClub0
 */
class Usuari extends CActiveRecord
{
    public $novaContrasenya; //Utilitzat per a gestionar la contrasenya
    
	/**
	 * Returns the static model of the specified AR class.
	 * @return Usuari the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'usuari';
	}

    /**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('nomUsuari, rol, novaContrasenya, nomReal', 'required', 'on'=>'insert'),
            array('nomUsuari, rol, nomReal', 'required', 'on'=>'update'),
			array('idClub', 'numerical', 'integerOnly'=>true),
			array('nomUsuari, nomReal', 'length', 'max'=>45),
            array('novaContrasenya', 'length', 'min'=>4, 'max'=>16, 'allowEmpty'=>FALSE, 'on'=>'insert'),
            array('novaContrasenya', 'length', 'min'=>4, 'max'=>16, 'allowEmpty'=>TRUE, 'on'=>'update'),
			array('rol', 'length', 'max'=>30),
            array('nomUsuari', 'unique', 'message'=>'El nom d\'usuari <b>{value}</b> ja està en ús.'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('idUsuari, nomUsuari, contrasenya, rol, nomReal, idClub', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'usuariClub' => array(self::BELONGS_TO, 'Club', 'idClub'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'idUsuari' => 'Codi',
			'nomUsuari' => 'Nom d\'usuari',
			'contrasenya' => 'Contrasenya',
			'rol' => 'Rol',
			'nomReal' => 'Nom real',
			'idClub' => 'Club al que pertany',
            'novaContrasenya'=>($this->isNewRecord ? 'Contrasenya' : 'Nova contrasenya'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('idUsuari',$this->idUsuari);
		$criteria->compare('nomUsuari',$this->nomUsuari,true);
		$criteria->compare('contrasenya',$this->contrasenya,true);
		$criteria->compare('rol',$this->rol,true);
		$criteria->compare('nomReal',$this->nomReal,true);
		$criteria->compare('idClub',$this->idClub);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
            'sort'=>array(
                'defaultOrder'=>array(
                    'nomUsuari'=>false, //false= ordre ascendent, true= ordre descendent
                )),
            'pagination'=>array(
                'pageSize'=>Yii::app()->params['itemsPagina']),
		));
	}
    
    public function getOpcionsRol() {
        return array(
            'admin'=>'Administrador/a',
            'operador'=>'Operador/a',
            'club'=>'Representant de club'
            );
    }
    
    public function beforeSave() {
        if (!empty($this->novaContrasenya))
            $this->contrasenya= md5($this->novaContrasenya);
        return TRUE;
    }
    
    public function getNomClub() {
        if ($this->idClub===NULL)
            return '(sense club)';
        else
            return $this->usuariClub->nom;
    }
}